package com.yyh.stu;

import java.util.*;

/*编写一个程序实现学员成绩管理，每个学员包括3门课的成绩，从键盘输入学员信息，包括学号、姓名、三门课成绩，计算出学员的平均成绩，按照学员平均成绩由大到小排序
插入功能：在排序后的学员成绩表中插入一个学员的信息，要求插入后仍然保持成绩表有序
删除功能：要求输入指定的学号，从学员信息表中删除该学员，删除后的成绩表保持有序*/
public class Test6 {
    public static void main(String[] args) {
        //创建集合容器
        List<Student1> list = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        // 1. 输入学生信息
        while (true) {
            System.out.println("请输入学号：");
            int id = scanner.nextInt();
            System.out.println("请输入姓名：");
            String name = scanner.next();
            System.out.println("请输入三门课成绩（用空格分隔）：");
            int score1 = scanner.nextInt();
            int score2 = scanner.nextInt();
            int score3 = scanner.nextInt();
            Student1 student = new Student1(id, name, score1, score2, score3);
            list.add(student);
            System.out.println("是否继续输入？(y/n)");
            String choice = scanner.next();
            if (choice.equalsIgnoreCase("n")) {
                break;
            }
        }
        // 2. 计算并显示平均成绩
        System.out.println("各学生平均成绩：");
        for (Student1 student : list) {
            System.out.println(student.getName() + "的平均成绩：" + student.calculateAverage());
        }
        // 3. 按平均成绩排序
        Collections.sort(list, (s1, s2) ->
                Double.compare(s2.calculateAverage(), s1.calculateAverage()));
        System.out.println("排序后的成绩表：");
        for (Student1 student : list) {
            System.out.println(student);
        }
        // 4. 插入功能（保持有序）
        System.out.println("=== 插入新学生 ===");
        System.out.println("请输入学号：");
        int newId = scanner.nextInt();
        System.out.println("请输入姓名：");
        String newName = scanner.next();
        System.out.println("请输入三门课成绩：");
        int newScore1 = scanner.nextInt();
        int newScore2 = scanner.nextInt();
        int newScore3 = scanner.nextInt();
        Student1 newStudent = new Student1(newId, newName, newScore1, newScore2, newScore3);
        // 有序插入
        boolean inserted = false;
        for (int i = 0; i < list.size(); i++) {
            if (newStudent.calculateAverage() > list.get(i).calculateAverage()) {
                list.add(i, newStudent);
                inserted = true;
                break;
            }
        }
        if (!inserted) {
            list.add(newStudent);
        }
        System.out.println("插入后的成绩表：");
        for (Student1 student : list) {
            System.out.println(student);
        }
        // 5. 删除功能
        System.out.println("=== 删除学生 ===");
        System.out.println("请输入要删除的学号：");
        int deleteId = scanner.nextInt();
        boolean removed = list.removeIf(student -> student.getId() == deleteId);
        if (removed) {
            System.out.println("删除成功！");
            // 删除后自动保持有序（因为原本就是有序的）
            System.out.println("删除后的成绩表：");
            for (Student1 student : list) {
                System.out.println(student);
            }
        } else {
            System.out.println("未找到该学号的学生！");
        }
    }

}
//创建Student类
class Student1{
    //属性
    //学号
    private int id;
    //姓名
    private String name;
    //成绩1
    private int score1;
    //成绩2
    private int score2;
    //成绩3
    private int score3;
    //构造方法
    public Student1(int id, String name, int score1, int score2, int score3) {
        this.id = id;
        this.name = name;
        this.score1 = score1;
        this.score2 = score2;
        this.score3 = score3;
    }
    //计算平均分
    public double calculateAverage() {
        return (score1 + score2 + score3) / 3.0;
    }
    // getter方法
    public int getId() { return id; }
    public String getName() { return name; }
    //toString方法
    @Override
    public String toString() {
        return String.format("学号：%d, 姓名：%s, 成绩：[%d, %d, %d], 平均分：%.2f",
                id, name, score1, score2, score3, calculateAverage());
    }
    //重写equals和hashCode方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student1 student1 = (Student1) o;
        return id == student1.id;
    }
    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
